#!/usr/bin/env python
# Authors: Han Wang#
#coding=utf-8
import rospy
import time
from geometry_msgs.msg import Twist, Point, Quaternion
import tf
from math import radians, copysign, sqrt, pow, pi, atan2,cos,sin
from tf.transformations import euler_from_quaternion
import numpy as np
from sensor_msgs.msg import LaserScan

msg = """
control your Robot!
-----------------------
this is leader
-----------------------
"""
class GotoPoint():
    def __init__(self):
        rospy.init_node('leader', anonymous=False)

        rospy.on_shutdown(self.shutdown)
        self.cmd_vel =rospy.Publisher('cmd_vel', Twist, queue_size=5)
        position = Point()
        r = rospy.Rate(10)
        self.tf_listener = tf.TransformListener()
        self.odom_frame = '/leader/odom'
        try:
            self.tf_listener.waitForTransform(self.odom_frame, '/leader/base_footprint', rospy.Time(), rospy.Duration(1.0))
            self.base_frame = '/leader/base_footprint'
        except (tf.Exception, tf.ConnectivityException, tf.LookupException):
            try:
                self.tf_listener.waitForTransform(self.odom_frame, '/leader/base_link', rospy.Time(), rospy.Duration(1.0))
                self.base_frame = '/leader/base_link'
            except (tf.Exception, tf.ConnectivityException, tf.LookupException):
                rospy.loginfo("Cannot find transform between odom and base_link or base_footprint")
                rospy.signal_shutdown("tf Exception")
#################################################################################################################################
        
        leader_vel=Twist()
        (position, rotation) = self.get_odom()
        leader_vel.linear.x=0.1
        #leader_vel.angular.z=0.1
        print position
        self.cmd_vel.publish(leader_vel)
#################################################################################################################################


     


    def get_odom(self):
        try:
            (trans, rot) = self.tf_listener.lookupTransform(self.odom_frame, self.base_frame, rospy.Time(0))
            rotation = euler_from_quaternion(rot)

        except (tf.Exception, tf.ConnectivityException, tf.LookupException):
            rospy.loginfo("TF Exception")
            return

        return (Point(*trans), rotation[2])

    def shutdown(self):
        self.cmd_vel.publish(Twist())
        rospy.sleep(1)
    def lidar(self,tb_pos):
        msg = rospy.wait_for_message("scan", LaserScan)
        LIDAR_ERR = 0.05
        LIDAR_MAX = 2
        obstacle=[]
        min_dis=3
        min_ang=0
        min_point=Point()
        for i in range(360):
            if i <= 90 or i > 270 and i!=0:
                obstacle_pos=Point()
                
                if msg.ranges[i] >= LIDAR_ERR and msg.ranges[i]<=LIDAR_MAX and msg.ranges[i]>0.17:
                    obstacle_pos.x=tb_pos.x+msg.ranges[i]*cos(i*2*pi/360)
                    obstacle_pos.y=tb_pos.y+msg.ranges[i]*sin(i*2*pi/360)
                    obstacle.append(obstacle_pos)
                    if msg.ranges[i] < min_dis:
                            min_dis = msg.ranges[i]
                            min_ang = i
        if min_dis<3:
            min_point.x=tb_pos.x+min_dis*cos(i*2*pi/360)
            min_point.y=tb_pos.y+min_dis*sin(i*2*pi/360)
            min_point.z=min_ang
        else:
            min_point.x=-1000
            min_point.y=-1000
        return min_point



if __name__ == '__main__':
    try:
        while not rospy.is_shutdown():
            print(msg)
            GotoPoint()

    except:
        rospy.loginfo("shutdown program.")

